﻿using System;
using System.Collections.Generic;
using System.Linq;
using FNN.SourcePoints;

namespace FNN.Helpers
{
    public class RegularCsvHelper : CsvHelper
    {
        /// <summary>
        ///     Конструктор по умолчанию
        /// </summary>
        /// <param name="fileName"></param>
        public RegularCsvHelper(string fileName) : base(fileName)
        {
        }

        /// <summary>
        ///     Преобразовать данные в массив точек
        /// </summary>
        /// <param name="headers">Заголовки</param>
        /// <param name="source">Содержимое</param>
        /// <returns>Массив точек</returns>
        public override List<SourcePoint> ProcessData(List<string> headers, List<string[]> source)
        {
            var result = new List<SourcePoint>();
            foreach (var row in source)
            {
                var newPoint = new CategorySourcePoint()
                {
                    Coordinates = new Dictionary<string, double>(),
                    CategoryId = Convert.ToInt32(row[0])
                };
                for (var i = 1; i < row.ToList().Count(); i++)
                {
                    newPoint.Coordinates.Add(headers[i], Convert.ToDouble(row[i].Replace(".", ",")));
                }
                result.Add(newPoint);
            }
            return result;
        }

        /// <summary>
        ///     Преобразовать данные в массив точек с задержкой
        /// </summary>
        /// <param name="headers">Заголовки</param>
        /// <param name="source">Содержимое</param>
        /// <param name="lag">Задержка</param>
        /// <returns>Массив точек</returns>
        public override List<SourcePoint> ProcessData(List<string> headers, List<string[]> source, int lag)
        {
            var result = new List<SourcePoint>();
            for (int j = lag; j < source.Count; j++)
            {
                var row = source[j];
                var laggedRow = source[j - lag];
                var newPoint = new CategorySourcePoint()
                {
                    Coordinates = new Dictionary<string, double>(),
                    CategoryId = Convert.ToInt32(row[0])
                };
                for (var i = 1; i < row.ToList().Count(); i++)
                {
                    newPoint.Coordinates.Add(headers[i], (Convert.ToDouble(row[i].Replace(".", ",")) / Convert.ToDouble(laggedRow[i].Replace(".", ","))) - 1);
                }
                result.Add(newPoint);
            }
            return result;
        }
    }
}
